var express = require('express');
var router = express.Router();
const moment = require('moment')

//分类列表
router.get('/product_category/list', function (req, res, next) {
    res.render('admin_tmpl/product_category/list.html', {});
});


//菜单添加
router.get('/product_category/add', async (req, res, next) => {
    var parentSQL = 'select * from t_product_category where delete_flag = 0 and parent is null'
    await common.prototype.operation(parentSQL).then(rest => {
        res.render('admin_tmpl/product_category/add.html', {parent: rest});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});

//修改分类
router.get('/product_category/edit', async (req, res, next) => {
    var id = req.query.id;
    var selectSQL = 'select * from t_product_category where delete_flag = 0 and id="' + id + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        var category = rest[0];
        res.render('admin_tmpl/product_category/edit.html', category);
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
});


/*
 * 分类列表
 * @return
 *
 */
router.post('/product_category/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeBelongto = req.body.search_like_belongto;
    if (likeBelongto) {
        condition = condition + 'and position("' + likeBelongto + '" IN belongto)'
    }
    SQL = 'select * from t_product_category where delete_flag = 0 ' + condition + '';
    await common.prototype.operation(SQL).then(async rest => {
        if (pageSize) {
            begin = (pageNumber - 1) * pageSize;
            condition = condition + 'order by id desc limit ' + begin + ',' + pageSize + ''
        }
        allData = rest;
        conditionSQL = 'select * from t_product_category where delete_flag = 0 ' + condition + ''
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 添加分类
 * @return
 *
 */
router.post('/product_category/addCategory', async (req, res, next) => {
    var data = req.body;
    var level;
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
       if (!data.order) {
        data.order = 0
    }
    var SQL = 'INSERT INTO `t_product_category`(title,parent,belongto,create_date,modify_date,delete_flag) VALUES("' + data.title + '","' + data.parent + '","' + data.parentname + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 修改分类
 * @param id
 * @param title
 * @param url
 * @param icon
 * @param orders
 * @param modify_date
 * @return
 *
 */
router.post('/product_category/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_product_category set title="' + data.title + '",name="' + data.name + '",type="' + data.type + '",modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 删除分类
 * @return
 *
 */
router.post('/product_category/delete', async (req, res, next) => {
    var id = req.body.id;
    await common.prototype.operation('select * from t_product_category where parent=' + id + ' ').then(async rest => {
        if (rest.length > 0) {
            res.send({
                type: 'error', content: '请先删除子节点'
            })
        } else {
            await common.prototype.operation('delete from t_product_category where id=' + id + ' ').then(async result => {
                res.send({
                    type: 'success', content: '删除成功'
                })
            }, (error) => {
                console.log(error)
                res.send({
                    type: 'error', content: '删除失败'
                })
            })
        }
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '删除失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});


module.exports = router;